package code.cando;

/**
 * author : yeswater
 * date : 2023/12/4
 * <p>
 * 最长回文子串  关于中心对称
 */

public class T005 {
    public static void main(String[] args) {
        String s = "ac";
        System.out.println(longestPalindrome(s));
    }

    // 暴力解法
    public static String longestPalindrome(String s) {
        int len = s.length();
        if (len < 2) {
            return s;
        }
        int begin = 0;
        int maxLen = 1;
        char[] arr = s.toCharArray();
        for (int i = 0; i < len - 1; i++) {
            for (int j = i + 1; j < len; j++) {
                if ((j - i + 1 > maxLen) && verify(i, j, arr)) {
                    maxLen = j - i + 1;
                    begin = i;
                }
            }
        }
        return s.substring(begin, begin + maxLen);
    }

    public static boolean verify(int l, int r, char[] arr) {
        while (l < r) {
            if (arr[l] != arr[r]) {
                return false;
            }
            l++;
            r--;
        }
        return true;
    }
}
